package com.huaxiafinance.management.service.impl;

import com.huaxiafinance.management.dto.response.QueryRoleDto;
import com.huaxiafinance.management.mapper.RolePermissionMapper;
import com.huaxiafinance.management.model.RolePermission;
import com.huaxiafinance.management.service.RolePermissionService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service("rolePermissionService")
public class RolePermissionServiceImpl implements RolePermissionService {

	private static final Logger log = Logger.getLogger(RolePermissionServiceImpl.class);

	@Autowired
	private RolePermissionMapper rolePermissionMapper;

	@Override
	public List<QueryRoleDto> queryRolePermission(Integer id) throws Exception {
		try {
			return rolePermissionMapper.select(id);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}

		return Collections.emptyList();
	}

	@Override
	public boolean addRolePermission(RolePermission rolePermission) throws Exception {
		int i = 0;

		try {
			i = rolePermissionMapper.insert(rolePermission);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return i > 0;
	}

	@Override
	public boolean updateRolePermission(RolePermission rolePermission) throws Exception {
		int i = 0;

		try {
			i = rolePermissionMapper.update(rolePermission);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return i > 0;
	}

	@Override
	public boolean delRolePermission(Integer id) throws Exception {
		int i = 0;

		try {
			i = rolePermissionMapper.delete(id);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return i > 0;
	}
}